package com.shoe.controller;

import com.shoe.pojo.Cart;
import com.shoe.service.ShoeCartService;

import com.shoe.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@CrossOrigin
@RequestMapping("cart")
public class CartController {

    @Autowired
    ShoeCartService shoeCartService;
    @Autowired
    ResponseResult responseResult;

    @ResponseBody
    @RequestMapping("cartall")
    public ResponseResult selectAll(Integer userid){
        List<Cart> shoeCartList = shoeCartService.selectAll(userid);
        if(shoeCartList.size() > 0){
            responseResult.setCode(1);
            responseResult.setMsg("购物车查询成功");
            responseResult.setData(shoeCartList);
        } else {
            responseResult.setCode(0);
            responseResult.setMsg("购物车查询失败");
//            responseResult.setData(null);
        }
        return responseResult;
    }

    @ResponseBody
    @RequestMapping("delOneCart")
    public int delById(Long cartId){
        //int count = shoeCartService.deleteById(cartId);
//        if(count > 0){
//            responseResult.setCode(1);
//            responseResult.setMsg("购物车删除成功");
//        }else {
//            responseResult.setCode(0);
//            responseResult.setMsg("购物车删除失败");
//        }
        return shoeCartService.deleteById(cartId);
    }


    @ResponseBody
    @RequestMapping("delCarts")
    public ResponseResult delMany(String ids){
       String[] arr = ids.split(",");
       int count = 0;
       for(String i : arr){
           count += shoeCartService.deleteById(Long.valueOf(i));
       }
       if(count == arr.length){
           responseResult.setCode(1);
           responseResult.setMsg("购物车删除成功");
           responseResult.setData(count);
       }else {
           responseResult.setCode(0);
           responseResult.setMsg("购物车删除失败");
           responseResult.setData(count);
       }
       return  responseResult;
    }

    @RequestMapping("addCart/{userId}/{goodId}/{goodMoney}/{goodSize}")
    public String addCartt(@PathVariable String userId,@PathVariable String goodId,@PathVariable String goodMoney,@PathVariable String goodSize){

        int a = shoeCartService.addCart(Long.valueOf(userId),Long.valueOf(goodId), Integer.valueOf(goodMoney),Integer.valueOf(goodSize));
        System.out.println(a);
        if(a == 1){
            return "redirect:http://localhost:63343/qiguai/html/single-goods.html?id="+goodId +";1";
        }
        return null;
    }

    @RequestMapping("judge")
    @ResponseBody
    public Integer judge(@RequestParam String userId ,@RequestParam String goodId){
        int x = shoeCartService.selectId(Long.valueOf(userId),Long.valueOf(goodId));
        System.out.println(x);

        return x;
    }



}
